﻿using System;
using System.Configuration;
using System.Reflection;
using System.Linq.Expressions;

namespace Titan
{
    internal static class ExceptionFactory
    {



        public static ArgumentNullException EntityNullException(string argumentName)
        {
            return new ArgumentNullException(argumentName, "实体对象为null");
        }



        public static InvalidOperationException ConnectionNotOpendException()
        {
            return new InvalidOperationException("数据库连接未打开，请先使用Open()方法打开数据库连接");
        }
        public static InvalidOperationException TransactionOpendException()
        {
            return new InvalidOperationException("事务已打开，在提交前不能重复打开事务");
        }
        public static InvalidOperationException TransactionNotOpendException()
        {
            return new InvalidOperationException("事务未打开，请先使用BeginTransaction()方法启动一个数据库事务");
        }

        public static InvalidOperationException NonMappingFoundException(string typeName)
        {
            return new InvalidOperationException(string.Format("针对IDynamicEntity类型，未找到实体名称为{0}的映射信息", typeName));
        }
        public static InvalidOperationException AttributeNotFoundException(Type type, Type attributeType)
        {
            return new InvalidOperationException(string.Format("类型{0}缺少{1}标注", type, attributeType));
        }
        public static InvalidOperationException AttributeNotFoundException(MemberInfo memberInfo, Type attributeType)
        {
            return new InvalidOperationException(string.Format("类型{0}的属性{1}缺少{2}标注", memberInfo.ReflectedType, memberInfo, attributeType));
        }
        public static InvalidOperationException TableAttributeNotFoundException(Type type, Type attributeType)
        {
            return new InvalidOperationException(string.Format("类型{0}有{2}标注但是缺少TableAttribute", type, attributeType));
        }
        public static InvalidOperationException StatementAttributeNotFoundException(Type type, Type attributeType)
        {
            return new InvalidOperationException(string.Format("类型{0}有{2}标注但是缺少StatementAttribute", type, attributeType));
        }
        public static InvalidOperationException ColumnAttributeNotFoundException(MemberInfo memberInfo, Type attributeType)
        {
            return new InvalidOperationException(string.Format("类型{0}的属性{1}有{2}标注但是缺少ColumnAttribute", memberInfo.ReflectedType, memberInfo, attributeType));
        }


        public static ArgumentException TypeMissingInTableMapping(string paramName, Type type)
        {
            return new ArgumentException(string.Format("tableMapping中不包含{0}的键", type), paramName);
        }


        public static InvalidOperationException FullTextSearchNotSupported(string propertyName)
        {
            return new InvalidOperationException(string.Format("属性{0}不支持全文索引或缺少FullTextSearch标注", propertyName));
        }

        public static ConfigurationErrorsException SectionNotFound(string sectionName)
        {
            return new ConfigurationErrorsException(string.Format("配置文件中未发现{0}配置", sectionName));
        }
        public static SettingsPropertyNotFoundException SettingsPropertyNotFound(string propertyName)
        {
            return new SettingsPropertyNotFoundException(string.Format("配置文件中未发现{0}属性", propertyName));
        }


        public static ArgumentOutOfRangeException PageIndexArgumentOutOfRangeException()
        {
            return new ArgumentOutOfRangeException("pageIndex", "pageIndex从1开始，参数必须>=1");
        }
        public static ArgumentOutOfRangeException PageSizeArgumentOutOfRangeException()
        {
            return new ArgumentOutOfRangeException("pageSize", "pageSize必须>=0");
        }

        public static NotSupportedException NotSupported(string message)
        {
            return new NotSupportedException(message);
        }

        public static NotSupportedException NotSupportedLambda(Expression expression)
        {
            return new NotSupportedException(string.Format("不支持的Lambda表达式：{0}", expression));
        }
        public static NotSupportedException NotSupportedLambdaMethod(Expression expression,string methodName)
        {
            return new NotSupportedException(string.Format("不支持的Lambda条件运算方法：{0},{1}", methodName,expression));
        }

        public static NotSupportedException NotSupportedLambdaProperty(Expression expression)
        {
            return new NotSupportedException(string.Format("不支持的Lambda属性表达式：{0},{1}", expression));
        }
         
    }
}
